#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
账本 - 余额退款功能使用示例
"""

from examples.base_example import BaseExample
from jlpay.services.ledger.balancesplit_service import BalanceSplitService
from jlpay.model.ledger.balancesplit import BalanceRefundRequest


class BalanceRefundExample(BaseExample):
    """余额退款示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.ledger.balancesplit.refund")
        self.service = BalanceSplitService(self.config)
    
    def build_balance_refund_request(self):
        """构建余额退款请求"""
        request = BalanceRefundRequest()
        
        # 设置请求参数
        request.set_org_code("50720711")
        request.set_mch_id("849581183510017")
        request.set_out_order_no("045e1314f7784f46bf9d590dc65c7")
        request.set_out_return_no("12e3045e13146bf9d590d12c65c11")
        request.set_rev_mch_id("849584307420004")
        request.set_amount(100)
        request.set_description("分账退回")
        
        return request
    
    def demo(self):
        """执行余额退款示例"""
        self.logger.info("开始执行余额退款示例")
        
        # 创建余额退款请求
        request = self.build_balance_refund_request()
        
        # 发送请求
        response = self.send_request_and_handle_response(
            self.service.refund_balance,
            "余额退款",
            request
        )
        
        # 处理响应结果
        if response and response.is_success():
            self.logger.info(f"响应报文：{response.to_dict()}")


if __name__ == "__main__":
    example = BalanceRefundExample()
    example.logger.info("=" * 50)
    example.logger.info("===== 余额退款示例 开始 =====")
    example.logger.info("=" * 50)
    
    example.demo()
    
    example.logger.info("=" * 50)
    example.logger.info("===== 余额退款示例执行完毕 =====")
    example.logger.info("=" * 50) 